package cn.itcast.jk.controller.cargo.outProduct;

import cn.itcast.jk.controller.BaseController;
import cn.itcast.jk.service.OutProductVOService;
import cn.itcast.jk.vo.OutProductVO;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class OutProductController extends BaseController {
    @Autowired
    OutProductVOService outProductVOService;
    @RequestMapping("/cargo/outProduct/toedit.action")
    public String toedit(){
        return "/cargo/outProduct/jOutProduct.jsp";
    }
    @RequestMapping("/cargo/outProduct/print.action")
    public String print(String inDate) throws IOException {
        Map map = new HashMap();
        map.put("inDate", inDate);
        int rowNum = 0;
        int cellNum = 0;
        List<OutProductVO> dataList = outProductVOService.find(map);
        OutputStream os = new FileOutputStream(new File("D:\\outProduct.xls"));     //创建文件
        HSSFWorkbook workbook = new HSSFWorkbook();     //创建工作簿
        HSSFSheet sheet1 = workbook.createSheet();      //创建工作表
        Row row1 = sheet1.createRow(rowNum++);          //创建行对象并定位到第一行(行数和列数都是从0开始)
        Cell cell1 = row1.createCell(0);        //创建一列并定位到单元格
        String[] str = new String[]{"客户","订单号","货号","数量","厂家名称","出货日期","船期","贸易条款"};
        for(int i = 0;i<str.length;i++){
            cell1 = row1.createCell(i);             //重新定位单元格
            cell1.setCellValue(str[i]);             //给单元格赋值
        }
        for(int i = 0;i<dataList.size();i++){
            row1 = sheet1.createRow(rowNum++);
            OutProductVO outProductVO = dataList.get(i);
            cell1 = row1.createCell(cellNum++);
            cell1.setCellValue(outProductVO.getCustomName());
            cell1 = row1.createCell(cellNum++);
            cell1.setCellValue(outProductVO.getContractNo());
            cell1 = row1.createCell(cellNum++);
            cell1.setCellValue(outProductVO.getProductNo());
            cell1 = row1.createCell(cellNum++);
            cell1.setCellValue(outProductVO.getCnumber());
            cell1 = row1.createCell(cellNum++);
            cell1.setCellValue(outProductVO.getFactoryName());
            cell1 = row1.createCell(cellNum++);
            cell1.setCellValue(outProductVO.getDeliveryPeriod());
            cell1 = row1.createCell(cellNum++);
            cell1.setCellValue(outProductVO.getShipTime());
            cell1 = row1.createCell(cellNum++);
            cell1.setCellValue(outProductVO.getTradeTerms());
            cellNum = 0;
        }
        workbook.write(os);                 //把工作簿写入文件
        os.flush();                         //刷新流
        os.close();                         //关闭流
        return "redirect:/cargo/contract/list.action";
    }
}
